package com.xy.tool.data.redis.core;

import java.util.Collection;
import java.util.Iterator;

public class Key {

    /**
     * default separator for key
     */
    public final static char DEFAULT_SEPARATOR = ':';

    protected char separator = DEFAULT_SEPARATOR;

    public void setSeparator(char separator) {
        if(separator > 0) {
            this.separator = separator;
        }
    }

    public char getSeparator() {
        return separator;
    }

    static String concat(char separator, String... values) {
        if(null == values || values.length == 0) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for(int i = 0, len = values.length, mask = len - 1; i < len; i++) {
            builder.append(values[i]);
            if(i < mask) {
                builder.append(separator);
            }
        }
        return builder.toString();
    }

    static String concat(char separator, Collection<String> values) {
        if(null == values || values.size() == 0) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        Iterator<String> it = values.iterator();
        for(int i = 0, len = values.size(), mask = len - 1; i < len; i++) {
            builder.append(it.next());
            if(i < mask) {
                builder.append(separator);
            }
        }
        return builder.toString();
    }

}
